import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export interface IUserInfo {
  avatar: string
  nickname: string
  organizationId: number
  type: number
  userId: number
  username: string
}

export const useUserInfo = defineStore('userinfo', () => {
  const cacheLoginInfo=localStorage.getItem('login')
  let userInfo
  let userScope
  let rolesCodes
  if(cacheLoginInfo){
    const {info,scopes,attributes}=JSON.parse(cacheLoginInfo)
    userInfo=info
    userScope=scopes
    rolesCodes=attributes.rolesCodes
  }

  const user = ref(userInfo)
  const scope = ref<string[]>(userScope)
  const roles = ref<string[]>(rolesCodes)
  const setUser=({info,scopes,attributes}:{info:IUserInfo,scopes:string[],attributes:{rolesCodes:string[]}})=>{
    user.value=info
    scope.value=scopes
    roles.value=attributes.rolesCodes
  }
  return {user,setUser,scope,roles}
})
